package com.ic.loan.activity

import android.os.Bundle
import com.blankj.utilcode.util.GsonUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ktx.immersionBar
import com.ic.loan.R
import com.ic.loan.bean.ImageBean
import com.ic.loan.http.ApiUrl
import com.ic.loan.http.HttpUtils
import com.ic.loan.http.ResultListener
import com.ic.loan.utils.GlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import kotlinx.android.synthetic.main.activity_refund.*
import kotlinx.android.synthetic.main.activity_refund.ivBack
import kotlinx.android.synthetic.main.activity_refund.tvSubmit
import java.io.File

class RefundActivity : BaseActivity() {
    private var imageUrl:String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarDarkFont(true)
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_refund)
        ivBack.setOnClickListener {
            onBackPressed()
        }
        tvSubmit.setOnClickListener {
            submit()
        }
        ivImage.setOnClickListener {
            selectPicture {
                Glide.with(this).load(it?.realPath).into(ivImage)
                updateImage(File(it?.realPath), 0)
            }
        }
    }

    private fun submit() {
        val content = edtContent.text.toString()
        if (content.isNullOrBlank()){
            return
        }
        showLoading()
        HttpUtils.postOkGo(mutableMapOf<String,Any>("content" to content)?.apply {
            imageUrl?.let { put("screenshot",it) }
        },ApiUrl.POST_CONTACT_REFUND,object : ResultListener() {
            override fun onSucceeded(json: String?) {

            }

            override fun onLoadFinish() {
                super.onLoadFinish()
                hintLoading()
            }
        },true)
    }

    private fun selectPicture(selectCallback: (LocalMedia?) -> Unit) {
        PictureSelector.create(this)
            .openGallery(PictureMimeType.ofImage())
            .imageEngine(GlideEngine.createGlideEngine())
            .isOriginalImageControl(true)
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: List<LocalMedia?>) {
                    // 结果回调
                    selectCallback.invoke(result[0])
                }

                override fun onCancel() {
                    // 取消
                }
            })
    }

    private fun updateImage(file: File, index: Int) {
        showLoading()
        HttpUtils.postOkGoForUpdataFile(
            mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            "Filedata",
            file,
            ApiUrl.POST_UPLOAD_IMG,
            object : ResultListener() {
                override fun onSucceeded(json: String?) {
                    val imageBean = GsonUtils.fromJson(json, ImageBean::class.java)
                    imageUrl = imageBean.path
                }

                override fun onLoadFinish() {
                    hintLoading()
                }

            })
    }



}